package cn.gaily.eduorder.service.impl;

import cn.gaily.commonutils.CourseInfoVoOrder;
import cn.gaily.commonutils.UcenterMemberOrder;
import cn.gaily.eduorder.entity.TOrder;
import cn.gaily.eduorder.feignclient.EduClient;
import cn.gaily.eduorder.feignclient.UserClient;
import cn.gaily.eduorder.mapper.TOrderMapper;
import cn.gaily.eduorder.service.TOrderService;
import cn.gaily.eduorder.util.OrderNoUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author xuchengwei
 * @since 2021-08-09
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Autowired
    private EduClient eduClient;
    @Autowired
    private UserClient userClient;

    @Override
    public String createOrder(String courseId, String userId) {
        CourseInfoVoOrder courseInfoOrder = eduClient.getCourseInfoToOrder(courseId);
        UcenterMemberOrder userInfoOrder = userClient.tokenToOrder(userId);
        TOrder order = new TOrder();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId); //课程id
        order.setCourseTitle(courseInfoOrder.getTitle());//课程名称
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherId());
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(userId);
        order.setMobile(userInfoOrder.getMobile());
        order.setNickname(userInfoOrder.getNickname());

        order.setStatus(0);  //订单状态（0：未支付 1：已支付）
        order.setPayType(1);  //支付类型 ，微信1

        baseMapper.insert(order);
        return order.getOrderNo();
    }
}
